package com.qing.springcloud.controller;

import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.qing.springcloud.feignService.StockFeignService;
import com.qing.springcloud.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
@RefreshScope(proxyMode = ScopedProxyMode.DEFAULT)
public class OrderController {


    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private StockFeignService stockFeignService;

    @Value("${name}")
    private String name;

    @Autowired
    private OrderService orderService;

    @GetMapping("/add")
    public String add(){
        String stock=restTemplate.getForObject("http://stock-server/stock/reduce?productId=1&num=2", String.class);
        return name+" 订单添加成功！"+stock;
    }

    @GetMapping("/add2")
    public String add2(){
        String stock=stockFeignService.reduce(1,2);
        return name+" 订单添加成功！"+stock;
    }

    @GetMapping("/create")
    public String createOrder(@RequestParam(value = "num",defaultValue = "1") Integer num){
        orderService.createOrder(name,"1",num);
        return name+" 订单添加成功！";
    }

    @SentinelRestTemplate()
    @GetMapping("/sleep")
    public String sleep(){
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "调用本接口会阻塞5秒";
    }
}
